package com.lianniu.web.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.pagination.PaginationInfo;
import com.lianniu.web.product.dao.IProductCateDao;
import com.lianniu.web.product.model.ProductCateVo;
import com.lianniu.web.product.service.IProductCateService;
import com.lianniu.web.util.StringUtil;

@Service
public class ProductCateServiceImpl implements IProductCateService {

	private final static Logger logger = Logger.getLogger(ProductCateServiceImpl.class);
	  
	@Resource
	IProductCateDao productCateDao;
	@Override
	public BaseResult selectByProductCateForPage(ProductCateVo curProductCateVo, PaginationInfo paginationInfo) {
	    logger.info("selectByProductCateForPage begin.");
	    if (!StringUtil.isEmpty(curProductCateVo.getCateName())) {
	    	curProductCateVo.setCateName(curProductCateVo.getCateName().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage cateName:" +curProductCateVo.getCateName());
	    if (!StringUtil.isEmpty(curProductCateVo.getparentId())) {
	    	curProductCateVo.setparentId(curProductCateVo.getparentId().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage parentId:" +curProductCateVo.getparentId());
	    if (!StringUtil.isEmpty(curProductCateVo.getCateId())) {
	    	curProductCateVo.setCateId(curProductCateVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductCateForPage CateId:" +curProductCateVo.getCateId());
	    logger.info("selectByProductCateForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    List<ProductCateVo> list=productCateDao.selectByProductCateForPage(curProductCateVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByProductCateForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(ProductCateVo curProductCateVo) {
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curProductCateVo.getCateId());
	  
	    return BaseResult.success(productCateDao.selectById(curProductCateVo.getCateId()));
	}


	@Override
	public BaseResult selectParentCate() {
	    logger.info("selectParentCate begin");
	    logger.info("selectParentCate parm: null");
	  
	    return BaseResult.success(productCateDao.getProductCateKeyValue());
	}

	@Override
	public BaseResult selectChildCateKV(String cateID) {
	    logger.info("selectChildCateKV begin");
	    logger.info("selectChildCateKV parm:"+cateID);
	  
	    return BaseResult.success(productCateDao.getProductCateChildKv(cateID));
	}

	@Override
	public BaseResult getByParentId(String parentId) {
		logger.info("getByParentId begin");
		logger.info("getByParentId parentId" +parentId);
		return BaseResult.success(productCateDao.getByParentId(parentId));
	}

    @Override
    public BaseResult selectByCateId(String cateId) {
        logger.info("selectByCateId begin");
        logger.info("selectByCateId cateId:"+cateId);
        return BaseResult.success(productCateDao.selectByCateId(cateId));
    }


}
